package com.ruoyi.project.monitor.job.task;

import com.ruoyi.project.jiaqin.downquotation.service.IDownQuotationService;
import com.ruoyi.project.jiaqin.news.mapper.NewsMapper;
import com.ruoyi.project.jiaqin.news.service.INewsService;
import com.ruoyi.project.jiaqin.question.mapper.JiaqinQuestionMapper;
import com.ruoyi.project.jiaqin.question.service.IJiaqinQuestionService;
import com.ruoyi.project.jiaqin.quotation.domain.QuotationTrend;
import com.ruoyi.project.jiaqin.quotation.service.IQuotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private IQuotationService iQuotationService;
    @Autowired
    private JiaqinQuestionMapper jiaqinQuestionMapper;

    @Autowired
    private IDownQuotationService iDownQuotationService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    /**
     * 新闻浏览量定时任务
     */
    public void NewsNumSeeHandler(){

        //查询新闻id列表
        List<String> list = newsMapper.selectNewsIds();
        //遍历ID列表,获取redis中的值 并减去 更改数据库浏览量

        for (String s :list) {
            String sa = redisTemplate.opsForValue().get(s);
            System.out.println(sa);
            if(sa == null){
                continue;
            }
            Long numSeeCache = Long.parseLong(sa);
            //如果浏览量为0不执行update语句
            if(numSeeCache == 0){
                continue;
            }
            redisTemplate.opsForValue().increment(s, 0-numSeeCache);
            newsMapper.updateNumSee(numSeeCache,s);
        }

    }

    /**
     * 问题浏览量定时任务
     */
    public void QuestionNumSeeHandler(){

        //查询新闻id列表
        List<String> list = jiaqinQuestionMapper.selectQuestionIds();
        //遍历ID列表,获取redis中的值 并减去 更改数据库浏览量

        for (String s :list) {
            String sa = redisTemplate.opsForValue().get(s);
            System.out.println(sa);
            if(sa == null){
                continue;
            }
            Long numSeeCache = Long.parseLong(sa);
            //如果浏览量为0不执行update语句
            if(numSeeCache == 0){
                continue;
            }
            redisTemplate.opsForValue().increment(s, 0-numSeeCache);
            jiaqinQuestionMapper.updateViews(numSeeCache,s);
        }
    }

    /**
     * 二级分类均价计算定时任务 每日两点计算均价插入数据库
     */
    public void twoClassAvg(){

        //查前一日全部均价,
        Integer i = iQuotationService.twoClassAvgStatistic();
        //遍历ID列表,获取redis中的值 并减去 更改数据库浏览量
    }

    /**
     * 二级分类均价计算定时任务 每日两点计算均价插入数据库
     */
    public void thrClassAvg(){

        //查前一日全部均价,
        Integer i = iQuotationService.thrClassAvgStatistic();

    }

    /**
     * 羽绒均价计算定时任务 每日两点计算均价插入数据库
     */
    public void downAvg(){

        //查前一日全部均价,
        Integer i = iDownQuotationService.downAvgStatistic();

    }


}
